/* source/core/eb_sprintf.c
 *
 * MIT License
 *
 * Copyright(c) 2023-present All contributors of SGL  
 *  
 * Document reference link: www.sgl-io.cn
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

#include <lib/eb_string.h>
#include <types.h>


int eb_snprintf(char *buffer, int32_t count, const char *format, va_list va) 
{
    int result = 0;
    int32_t pos = 0;

    if (buffer == NULL || format == NULL) {
        return -1;
    }

    if (count == 0) {
        return -1;
    }

    while (*format != '\0' && pos < count - 1) {
        if (*format == '%' && *(format + 1) != '%') {
            switch (*(format + 1)) {
            case 's':
                char *s = va_arg(va, char *);
                while (*s != '\0' && pos < count - 1) {
                    buffer[pos++] = *s++;
                    result++;
                }
                break;

            case 'd':
                int d = va_arg(va, int);
                eb_int64_to_str(d, buffer + pos);
                pos += eb_strlen(buffer + pos);
                result += eb_strlen(buffer + pos);
                break;

            case 'x':
                size_t x = va_arg(va, size_t);
                eb_int64_to_hexstr(x, buffer + pos);
                pos += eb_strlen(buffer + pos);
                result += eb_strlen(buffer + pos);
                break;

            case 'p':
                void *p = va_arg(va, void *);
                eb_uint64_to_hexstr((size_t)p, buffer + pos);
                pos += eb_strlen(buffer + pos);
                result += eb_strlen(buffer + pos);
                break;

            default:
                buffer[pos++] = *format;
                result++;
            }
            format += 2;
        }
        else {
            buffer[pos++] = *format;
            format++;
            result++;
        }
    }

    buffer[pos] = '\0';

    va_end(va);

    if (pos >= count - 1) {
        return -1;
    }

    return result;
}


/**
 * @brief Format string, this function may affect system real-time performance. 
 *        Please use this function with caution when calling
 * 
 * @param buffer:  Formatted output buffer
 * @param Content that needs to be formatted
 * 
 * @return Formatted length
*/
int eb_sprintf(char * buffer, const char * format, ...)
{
    int ret = -1;

    va_list va;
    va_start(va, format);
    ret = eb_snprintf(buffer, sizeof(buffer), format, va);
    va_end(va);

    return ret;
}
